import requests
import re
import numpy
import ConnectMySql
from bs4 import BeautifulSoup

page = 0
conn, cursor = ConnectMySql.openConn()


def getSinger(url):
    response = requests.get(url).text
    imgReg = r'<img src="(.*?)"'
    imgList = re.findall(imgReg, response, re.S)
    # print(imgList)
    idReg = r'<div class="artistnav" data-id="(.*?)">'
    idList = re.findall(idReg, response, re.S)
    # print(idList)
    soup = BeautifulSoup(response)
    nameList = soup.findAll("a", class_="a_name")
    # print(nameList)
    for i in range(len(idList)):
        cursor.execute("select sId from singer")
        sIdArray = cursor.fetchall()
        sIdArray = numpy.array(sIdArray)
        if idList[i] not in sIdArray:
            print(nameList[i].text)
            cursor.execute("insert into singer(sId,sName,sImg) VALUES (%s,%s,%s)", (idList[i], nameList[i].text, imgList[i]))
            conn.commit()


while page < 100:
    url = "http://www.kuwo.cn/artist/indexAjax?category=0&prefix=&pn=" + str(page)
    getSinger(url)
    page += 1


ConnectMySql.closeConn(conn,cursor)





